package com.test.mooan.labama.bean_boom.config_info_boom;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.test.mooan.labama.utils_boom.Stringutil_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class AppShowInfo_boom {

    private static class Inner {
        private static AppShowInfo_boom instance = new AppShowInfo_boom();
    }

    private AppShowInfo_boom() {}

    public static AppShowInfo_boom getInstance() {
        return Inner.instance;
    }

    private Customize customize;
    private ArrayList<WalletChannel> walletChannels;

    public static void parse(JSONObject objJson) {
        //if (objJson==null) return;
        Gson gson = new Gson();
        Inner.instance = gson.fromJson(objJson.toString(), AppShowInfo_boom.class);
        if (Inner.instance.customize == null)
            Inner.instance.customize = gson.fromJson(objJson.optString("customize"), Customize.class);
    }

    public String[] gMobiles() {
        if (customize == null) return new String[0];
        return customize.gMobile();
    }

    public boolean showSysReview(){
        if (customize==null) return false;
        return customize.showSysReview();
    }

    public String[] gEmails() {
        if (customize == null) return new String[0];
        return customize.gEmail();
    }

    public String[] gHome() {
        if (customize==null||customize.permissions==null) return new String[0];
        ArrayList<String> permission = customize.permissions.gHome();
        if (permission == null) return new String[0];
        String[] result = new String[permission.size()];
        for (int i = 0; i < permission.size(); i++) {
            result[i] = permission.get(i);
        }
        return result;
    }

    public String gTransferDelayImportant() {
        if (customize == null) return "";
        return Stringutil_boom.getSafeString(customize.gTransferDelayImportant());
    }

    public String gRejectPeriod() {
        if (customize == null) return "";
        return customize.gRejectPeriod();
    }

    public String isTestAccount(String mobile) {
        if (customize == null || customize.accList == null || customize.accList.size() < 1) return "";

        for (int i=0;i<customize.accList.size();i++){
            String accListJsStr = customize.accList.get(i).toString();
            try {
                JSONObject jsonObject = new JSONObject(accListJsStr);
                if (jsonObject.optString("accM").equalsIgnoreCase(mobile))
                    return jsonObject.optString("accTk");
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }

        return "";
    }

    public boolean isTestAccount(){
//        if (!UserInfoHelper_boom.getInstance().getIsAdSource()) return true;
        return !TextUtils.isEmpty(isTestAccount(UserInfoHelper_boom.gInstance().gMobile()));
    }

    /**
     * 是否需要申请UsageStats权限
     */
    public boolean needRequestUsageStatsPermission() {
        if (customize == null) return false;
        if (TextUtils.isEmpty(customize.usageStatsPermission)) {
            return false;
        } else {
            return customize.usageStatsPermission.equalsIgnoreCase("YES");
        }
    }

    public String gCustomerServiceHint(){
        if (customize == null) return "";
        return customize.gCustomerServiceHint();
    }

    public static class Customize {

        private String homeAmount;
        private String showSysReview;
        private String noPointRecord;
        private String companyAddress;
        private String mobile;
        private String company;
        private String afKey;
        private String email;
        private String repayType;
        private String rejectPeriod;
        private JsonArray walletChannels;
        private String transferDelayImportant;
        private JsonArray accList;
        private AppShowPermissions_boom permissions;

        private String customerServiceHint;

        /**
         * if YES, request PACKAGE_USAGE_STATS permission, or do nothing
         */
        private String usageStatsPermission;
        /**
         * usage stats days
         */
        private int usageStatsDays = 7;

        private Customize() {}

        public String[] gMobile() {
            return Stringutil_boom.getSafeString(mobile).split(",");
        }

        public boolean showSysReview(){
            return Stringutil_boom.getSafeString(showSysReview).equalsIgnoreCase("YES");
        }

        public String[] gEmail() {
            return Stringutil_boom.getSafeString(email).split(",");
        }

        public String gRejectPeriod() {
            return Stringutil_boom.getSafeString(rejectPeriod);
        }

        public JsonArray gWalletChannels() {
            return walletChannels;
        }

        public String gTransferDelayImportant() {
            return Stringutil_boom.getSafeString(transferDelayImportant);
        }

        public String gCustomerServiceHint() {
            if (TextUtils.isEmpty(customerServiceHint)) return "";
            return customerServiceHint;
        }
    }

    private void initWalletChannels() {
        walletChannels = new ArrayList<>();
        JsonArray jsonArray = customize.gWalletChannels();
        Gson gson = new Gson();
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                WalletChannel walletChannel = gson.fromJson(jsonArray.get(i).toString(), WalletChannel.class);
                walletChannels.add(walletChannel);
            }
        }

        //获取图标
        Threadutils_boom.getInstance().runOnChildThread(() -> {
            for (WalletChannel walletChannel : walletChannels) {
                try {
                    if (!TextUtils.isEmpty(walletChannel.gLogo())) {
                        URL url = new URL(walletChannel.gLogo());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode() == 200) {
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            walletChannel.sBitmap(bitmap);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ArrayList<WalletChannel> gWalletChannels() {
        if (walletChannels == null) {
            initWalletChannels();
        }
        return walletChannels;
    }

    public static class WalletChannel {

        private String desc;
        private String value;
        private String logo;

        private Bitmap bitmap;

        public String gDesc() {
            return Stringutil_boom.getSafeString(desc);
        }

        public String gValue() {
            return Stringutil_boom.getSafeString(value);
        }

        public String gLogo() {
            return Stringutil_boom.getSafeString(logo);
        }

        public Bitmap gBitmap() {
            return bitmap;
        }

        public void sBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
        }

    }

}
